package leetcode;

import com.sun.tools.hat.internal.model.Root;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LeetCode94 {

    List<Integer> result = new ArrayList<>();

    //使用递归解题
    public List<Integer> inorderTraversal1(TreeNode root) {
        walk(root);
        return result;
    }

    private void walk(TreeNode node) {
        if (node == null)
            return;
        walk(node.left);
        result.add(node.val);
        walk(node.right);
    }

    //------------------------------------------------------------------
    //使用非递归
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            while (curr != null) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            ans.add(curr.val);
            curr = curr.right;
        }
        return ans;
    }
}
